<template>
  <el-input v-if="nodeFilter" v-model="filterText" placeholder="筛选节点" style="margin-bottom: 12px" />
  <el-tree ref="treeRef" :data="dicData" :node-key="nodeKey" :props="treeProps" :default-expand-all="defaultExpandAll" :filter-node-method="filterNode" @node-click="handleNodeClick"> </el-tree>
</template>

<script>
import { customApiExecute, modelApiExecute, localApiExecute, getParamsWithMerge } from '../../utils/common-methods';
import { useAnvilCodeStore } from '../../store/modules/code';

export default {
  name: 'DynamicTree',
  // inheritAttrs: false,
  props: {
    treeProps: {
      type: Object,
      default: () => {}
    },
    nodeKey: {
      type: String,
      default: ''
    },
    defaultExpandAll: {
      type: Boolean,
      default: false
    },
    nodeFilter: {
      type: Boolean,
      default: false
    },
    dicOption: {
      type: String,
      default: ''
    },
    dicRes: {
      type: String,
      default: ''
    },
    ajaxSetting: {
      type: Object,
      default: () => {}
    },
    staticJson: {
      type: String,
      default: ''
    },
    nodeClick: {
      type: Function,
      default: () => {}
    }
  },
  data() {
    return {
      dicData: [],
      filterText: ''
    };
  },
  watch: {
    filterText(newVal) {
      if (this.nodeFilter) {
        this.$refs.treeRef.filter(newVal);
      }
    }
  },
  mounted() {
    this.getDynamicTreeData();
  },
  methods: {
    asyncLoad() {
      this.getDynamicTreeData();
    },
    getDynamicTreeData() {
      try {
        if (this.dicOption == 'static') {
          this.dicData = JSON.parse(this.staticJson);
        } else if (this.dicOption == 'remote' && this.ajaxSetting.url) {
          const { type, url, method, contentType, hookBefore, hookAfter, headers, params, noLoading } = this.ajaxSetting;

          const anvilCodeStore = useAnvilCodeStore();
          const { instance } = anvilCodeStore.getPageForm(this.$attrs.pageId);
          const callback = (response) => {
            let res = response;
            if (hookAfter) {
              console.log('调用后置钩子', hookAfter);
              // 获取当前表单实例
              res = instance[hookAfter](response);
            }
            let dicts = [];
            if (this.dicRes) {
              dicts = eval(this.dicRes);
            } else {
              dicts = res;
            }

            console.log('dicts', dicts);
            this.dicData = dicts;
          };

          let Header = {},
            Param = {};

          if (headers) {
            headers.map((item) => {
              Header[item.key] = item.value;
            });
          }

          Param = getParamsWithMerge(this, params);

          if (hookBefore) {
            console.log('调用前置钩子', hookBefore);
            Param = instance[hookBefore](Param);
          }

          const config = {
            url,
            method,
            contentType,
            headers: Header,
            params: Param,
            callback
          };

          switch (type) {
            case 'ajax':
              return customApiExecute(config, { noLoading });
            case 'ajaxModel':
              return modelApiExecute(config, { noLoading });
            case 'ajaxLocal':
              return localApiExecute(config, { noLoading });
          }
        }
      } catch (e) {
        console.log('getTreeSelectData Error!', e);
      }
    },
    // 树节点筛选
    filterNode(value, data) {
      if (!value) return true;
      return data[this.treeProps.label].includes(value);
    },
    getCurrentKey() {
      return this.$refs.treeRef.getCurrentKey();
    },
    getCurrentNode() {
      return this.$refs.treeRef.getCurrentNode();
    },
    handleNodeClick() {
      this.nodeClick();
    }
  }
};
</script>


